<?php

namespace App\Transformers;

use League\Fractal\TransformerAbstract;
use App\Models\StaffDormAdjust;

/**
 * Class StaffDormAdjustTransformer.
 *
 * @package namespace App\Transformers;
 */
class StaffDormAdjustTransformer extends TransformerAbstract
{
    /**
     * Transform the StaffDormAdjust entity.
     *
     * @param \App\Models\StaffDormAdjust $model
     *
     * @return array
     */
    public function transform(StaffDormAdjust $model)
    {
        return [
            'id'         => (int) $model->id,
            'company' => $model->company,
            'staff_id' => $model->staff_id,
            'staff_name' => $model->staff->name,
            'staff_card_num' => $model->staff->card_num,
            'staff_id_card' => $model->staff->id_card,
            'staff_sex' => $model->staff->sex ? ($model->staff->sex == 1 ? '男' : '女') : '',
            'dorm_code_from' => optional($model->dormFrom)->code,
            'dorm_code_to' => optional($model->dormTo)->code,
            'adjust_reason' => $model->adjust_reason,
            'adjust_type' => $this->getAdjustType($model),
            'adjust_date' => $model->adjust_date,
            'created_at' => $model->created_at->format('Y-m-d H:i:s'),
            'updated_at' => $model->updated_at->format('Y-m-d H:i:s')
        ];
    }
    public function getAdjustType(StaffDormAdjust $model)
    {
        if ($model->dorm_id_from && $model->dorm_id_to) {
            return '调整';
        } else if ($model->dorm_id_to) {
            return '入住';
        } else {
            return '移出';
        }
    }
}
